package com.xbongbong.pro.dataset.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.vo.ExportFormDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.dataset.service.DataSetChartExportService;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartPenetrateResultDTO;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 数据集图表导出操作
 *
 * @author zyy
 * @date 2022/11/24/ 13:59:00
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/dsValue/export")
public class DataSetChartExportController {

    @Resource
    private DataSetChartExportService dataSetChartExportService;

    /**
     * 导出数据集图表的数据
     *
     * @param chartResultDTO
     * @param br
     * @return java.lang.String
     * @author zyy
     * @date 2022/11/24
     * @version v1.0
     */
    @RequestMapping(value = "/data")
    public String exportChartData(@RequestBody ChartPenetrateResultDTO chartResultDTO, BindingResult br){
        XbbResponse<ExportFormDataVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ExportFormDataVO exportFormDataVO = dataSetChartExportService.exportChartData(chartResultDTO);
                response = new XbbResponse<>(exportFormDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.IgnoreNonFieldGetter);
    }
}
